package com.open.capacity.prints.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.open.capacity.commons.CodeEnum;
import com.open.capacity.commons.PageResult;
import com.open.capacity.prints.dao.BillGhsfptDao;
import com.open.capacity.prints.entity.BillGhsfpt;
import com.open.capacity.prints.service.BillGhsfptService;
import com.open.capacity.prints.util.ChineseYuanUtil;
import com.open.capacity.prints.util.uuid.UUIDUtils;
import com.open.capacity.utils.BigDecimalUtil;
import com.open.capacity.utils.DateUtil;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
public class BillGhsfptServiceImpl implements BillGhsfptService {

    @Autowired
    private BillGhsfptDao billGhsfptDao;

    /**
     * 添加
     *
     * @param billGhsfpt
     */
    public int save(BillGhsfpt billGhsfpt) {
        billGhsfpt.setId(UUIDUtils.getOriginalUUIDWithoutBar());
        billGhsfpt.setCreateTime(new Date());

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(DateUtil.parseDate2(billGhsfpt.getDjrq().replaceAll("-", "").replaceAll(" ", "")));                    //放入Date类型数据
        billGhsfpt.setYear(calendar.get(Calendar.YEAR));                    //获取年份
        billGhsfpt.setMonth(String.valueOf(calendar.get(Calendar.MONTH)));                    //获取月份
        billGhsfpt.setDay(String.valueOf(calendar.get(Calendar.DATE)));                    //获取日

        BigDecimal money = billGhsfpt.getCost1()
                .add(BigDecimalUtil.getData(billGhsfpt.getCost2()))
                .add(BigDecimalUtil.getData(billGhsfpt.getCost3()))
                .add(BigDecimalUtil.getData(billGhsfpt.getCost4()))
                .add(BigDecimalUtil.getData(billGhsfpt.getCost5()))
                .add(BigDecimalUtil.getData(billGhsfpt.getCost6()))
                .add(BigDecimalUtil.getData(billGhsfpt.getFee1()))
                .add(BigDecimalUtil.getData(billGhsfpt.getFee2()))
                .add(BigDecimalUtil.getData(billGhsfpt.getFee3()))
                .add(BigDecimalUtil.getData(billGhsfpt.getFee4()))
                .add(BigDecimalUtil.getData(billGhsfpt.getFee5()))
                .add(BigDecimalUtil.getData(billGhsfpt.getFee6()));
        billGhsfpt.setMoney2(money);

        billGhsfpt.setMoney1(ChineseYuanUtil.number2CNMontrayUnit(billGhsfpt.getMoney2()));

        return billGhsfptDao.save(billGhsfpt);
    }

    /**
     * 修改
     *
     * @param billGhsfpt
     */
    public int update(BillGhsfpt billGhsfpt) {
        billGhsfpt.setCreateTime(new Date());
        Calendar calendar = Calendar.getInstance();

        calendar.setTime(DateUtil.parseDate2(billGhsfpt.getDjrq().replaceAll("-", "").replaceAll(" ", "")));                    //放入Date类型数据
        billGhsfpt.setYear(calendar.get(Calendar.YEAR));                    //获取年份
        billGhsfpt.setMonth(String.valueOf(calendar.get(Calendar.MONTH)));                    //获取月份
        billGhsfpt.setDay(String.valueOf(calendar.get(Calendar.DATE)));                    //获取日

        BigDecimal money = billGhsfpt.getCost1()
                .add(BigDecimalUtil.getData(billGhsfpt.getCost2()))
                .add(BigDecimalUtil.getData(billGhsfpt.getCost3()))
                .add(BigDecimalUtil.getData(billGhsfpt.getCost4()))
                .add(BigDecimalUtil.getData(billGhsfpt.getCost5()))
                .add(BigDecimalUtil.getData(billGhsfpt.getCost6()))
                .add(BigDecimalUtil.getData(billGhsfpt.getFee1()))
                .add(BigDecimalUtil.getData(billGhsfpt.getFee2()))
                .add(BigDecimalUtil.getData(billGhsfpt.getFee3()))
                .add(BigDecimalUtil.getData(billGhsfpt.getFee4()))
                .add(BigDecimalUtil.getData(billGhsfpt.getFee5()))
                .add(BigDecimalUtil.getData(billGhsfpt.getFee6()));
        billGhsfpt.setMoney2(money);

        billGhsfpt.setMoney1(ChineseYuanUtil.number2CNMontrayUnit(billGhsfpt.getMoney2()));
        return billGhsfptDao.update(billGhsfpt);
    }


    /**
     * 删除
     *
     * @param id
     */
    public int delete(String id) {
        return billGhsfptDao.delete(id);
    }

    /**
     * 查询记录
     *
     * @param id
     * @return
     */
    public BillGhsfpt findById(String id) {
        BillGhsfpt item = billGhsfptDao.findById(id);
        //总额
        BigDecimal resut = item.getMoney2();
        Integer million5Count = resut.divide(BigDecimal.valueOf(100000000)).intValue();
        item.setMillion5(million5Count);
        resut = resut.subtract(BigDecimal.valueOf(million5Count * 100000000));

        Integer million4Count = resut.divide(BigDecimal.valueOf(10000000)).intValue();
        item.setMillion4(million4Count);
        resut = resut.subtract(BigDecimal.valueOf(million4Count * 10000000));


        Integer million3Count = resut.divide(BigDecimal.valueOf(1000000)).intValue();
        item.setMillion3(million3Count);
        resut = resut.subtract(BigDecimal.valueOf(million3Count * 1000000));

        Integer million2Count = resut.divide(BigDecimal.valueOf(100000)).intValue();
        item.setMillion2(million2Count);
        resut = resut.subtract(BigDecimal.valueOf(million2Count * 100000));

        Integer million1Count = resut.divide(BigDecimal.valueOf(10000)).intValue();
        item.setMillion1(million1Count);
        resut = resut.subtract(BigDecimal.valueOf(million1Count * 10000));

        Integer thousandCount = resut.divide(BigDecimal.valueOf(1000)).intValue();
        item.setThousand(thousandCount);
        resut = resut.subtract(BigDecimal.valueOf(thousandCount * 1000));

        Integer hundredCount = resut.divide(BigDecimal.valueOf(100)).intValue();
        item.setHundred(hundredCount);
        resut = resut.subtract(BigDecimal.valueOf(hundredCount * 100));

        Integer tenCount = resut.divide(BigDecimal.valueOf(10)).intValue();
        item.setTen(tenCount);
        resut = resut.subtract(BigDecimal.valueOf(tenCount * 10));

        Integer yuanCount = resut.divide(BigDecimal.valueOf(1)).intValue();
        item.setYuan(yuanCount);
        resut = resut.subtract(BigDecimal.valueOf(yuanCount * 1));

        Integer jiaoCount = resut.divide(BigDecimal.valueOf(0.1)).intValue();
        item.setJiao(jiaoCount);
        resut = resut.subtract(BigDecimal.valueOf(jiaoCount * 0.1));

        Integer fenCount = resut.divide(BigDecimal.valueOf(0.01)).intValue();
        item.setFen(fenCount);


        //项目1
        if (StringUtils.isNotEmpty(item.getProject1())) {
            BigDecimal p1 = BigDecimalUtil.getData(item.getCost1()).add(BigDecimalUtil.getData(item.getFee1()));

            Integer p1million3Count = p1.divide(BigDecimal.valueOf(1000000)).intValue();
            item.setMillion31(p1million3Count);
            p1 = p1.subtract(BigDecimal.valueOf(p1million3Count * 1000000));

            Integer p1million2Count = p1.divide(BigDecimal.valueOf(100000)).intValue();
            item.setMillion21(p1million2Count);
            p1 = p1.subtract(BigDecimal.valueOf(p1million2Count * 100000));

            Integer p1million1Count = p1.divide(BigDecimal.valueOf(10000)).intValue();
            item.setMillion11(p1million1Count);
            p1 = p1.subtract(BigDecimal.valueOf(p1million1Count * 10000));

            Integer p1thousandCount = p1.divide(BigDecimal.valueOf(1000)).intValue();
            item.setThousand1(p1thousandCount);
            p1 = p1.subtract(BigDecimal.valueOf(p1thousandCount * 1000));

            Integer p1hundredCount = p1.divide(BigDecimal.valueOf(100)).intValue();
            item.setHundred1(p1hundredCount);
            p1 = p1.subtract(BigDecimal.valueOf(p1hundredCount * 100));

            Integer p1tenCount = p1.divide(BigDecimal.valueOf(10)).intValue();
            item.setTen1(p1tenCount);
            p1 = p1.subtract(BigDecimal.valueOf(p1tenCount * 10));

            Integer p1yuanCount = p1.divide(BigDecimal.valueOf(1)).intValue();
            item.setYuan1(p1yuanCount);
            p1 = p1.subtract(BigDecimal.valueOf(p1yuanCount * 1));

            Integer p1jiaoCount = p1.divide(BigDecimal.valueOf(0.1)).intValue();
            item.setJiao1(p1jiaoCount);
            p1 = p1.subtract(BigDecimal.valueOf(p1jiaoCount * 0.1));

            Integer p1fenCount = p1.divide(BigDecimal.valueOf(0.01)).intValue();
            item.setFen1(p1fenCount);
        }

        //项目2
        if (StringUtils.isNotEmpty(item.getProject2())) {
            BigDecimal p2 = BigDecimalUtil.getData(item.getCost2()).add(BigDecimalUtil.getData(item.getFee2()));

            Integer p2million3Count = p2.divide(BigDecimal.valueOf(1000000)).intValue();
            item.setMillion32(p2million3Count);
            p2 = p2.subtract(BigDecimal.valueOf(p2million3Count * 1000000));

            Integer p2million2Count = p2.divide(BigDecimal.valueOf(100000)).intValue();
            item.setMillion22(p2million2Count);
            p2 = p2.subtract(BigDecimal.valueOf(p2million2Count * 100000));

            Integer p2million1Count = p2.divide(BigDecimal.valueOf(10000)).intValue();
            item.setMillion12(p2million1Count);
            p2 = p2.subtract(BigDecimal.valueOf(p2million1Count * 10000));

            Integer p2thousandCount = p2.divide(BigDecimal.valueOf(1000)).intValue();
            item.setThousand2(p2thousandCount);
            p2 = p2.subtract(BigDecimal.valueOf(p2thousandCount * 1000));

            Integer p2hundredCount = p2.divide(BigDecimal.valueOf(100)).intValue();
            item.setHundred2(p2hundredCount);
            p2 = p2.subtract(BigDecimal.valueOf(p2hundredCount * 100));

            Integer p2tenCount = p2.divide(BigDecimal.valueOf(10)).intValue();
            item.setTen2(p2tenCount);
            p2 = p2.subtract(BigDecimal.valueOf(p2tenCount * 10));

            Integer p3yuanCount = p2.divide(BigDecimal.valueOf(1)).intValue();
            item.setYuan2(p3yuanCount);
            p2 = p2.subtract(BigDecimal.valueOf(p3yuanCount * 1));

            Integer p2jiaoCount = p2.divide(BigDecimal.valueOf(0.1)).intValue();
            item.setJiao2(p2jiaoCount);
            p2 = p2.subtract(BigDecimal.valueOf(p2jiaoCount * 0.1));

            Integer p2fenCount = p2.divide(BigDecimal.valueOf(0.01)).intValue();
            item.setFen2(p2fenCount);
        }

        //项目3
        if (StringUtils.isNotEmpty(item.getProject3())) {
            BigDecimal p3 = BigDecimalUtil.getData(item.getCost3()).add(BigDecimalUtil.getData(item.getFee3()));

            Integer p3million3Count = p3.divide(BigDecimal.valueOf(1000000)).intValue();
            item.setMillion33(p3million3Count);
            p3 = p3.subtract(BigDecimal.valueOf(p3million3Count * 1000000));

            Integer p3million2Count = p3.divide(BigDecimal.valueOf(100000)).intValue();
            item.setMillion23(p3million2Count);
            p3 = p3.subtract(BigDecimal.valueOf(p3million2Count * 100000));

            Integer p3million1Count = p3.divide(BigDecimal.valueOf(10000)).intValue();
            item.setMillion13(p3million1Count);
            p3 = p3.subtract(BigDecimal.valueOf(p3million1Count * 10000));

            Integer p3thousandCount = p3.divide(BigDecimal.valueOf(1000)).intValue();
            item.setThousand3(p3thousandCount);
            p3 = p3.subtract(BigDecimal.valueOf(p3thousandCount * 1000));

            Integer p3hundredCount = p3.divide(BigDecimal.valueOf(100)).intValue();
            item.setHundred3(p3hundredCount);
            p3 = p3.subtract(BigDecimal.valueOf(p3hundredCount * 100));

            Integer p3tenCount = p3.divide(BigDecimal.valueOf(10)).intValue();
            item.setTen3(p3tenCount);
            p3 = p3.subtract(BigDecimal.valueOf(p3tenCount * 10));

            Integer p3yuanCount = p3.divide(BigDecimal.valueOf(1)).intValue();
            item.setYuan3(p3yuanCount);
            p3 = p3.subtract(BigDecimal.valueOf(p3yuanCount * 1));

            Integer p3jiaoCount = p3.divide(BigDecimal.valueOf(0.1)).intValue();
            item.setJiao3(p3jiaoCount);
            p3 = p3.subtract(BigDecimal.valueOf(p3jiaoCount * 0.1));

            Integer p3fenCount = p3.divide(BigDecimal.valueOf(0.01)).intValue();
            item.setFen3(p3fenCount);
        }

        //项目4
        if (StringUtils.isNotEmpty(item.getProject4())) {
            BigDecimal p4 = BigDecimalUtil.getData(item.getCost4()).add(BigDecimalUtil.getData(item.getFee4()));

            Integer p4million3Count = p4.divide(BigDecimal.valueOf(1000000)).intValue();
            item.setMillion34(p4million3Count);
            p4 = p4.subtract(BigDecimal.valueOf(p4million3Count * 1000000));

            Integer p4million2Count = p4.divide(BigDecimal.valueOf(100000)).intValue();
            item.setMillion24(p4million2Count);
            p4 = p4.subtract(BigDecimal.valueOf(p4million2Count * 100000));

            Integer p4million1Count = p4.divide(BigDecimal.valueOf(10000)).intValue();
            item.setMillion14(p4million1Count);
            p4 = p4.subtract(BigDecimal.valueOf(p4million1Count * 10000));

            Integer p4thousandCount = p4.divide(BigDecimal.valueOf(1000)).intValue();
            item.setThousand4(p4thousandCount);
            p4 = p4.subtract(BigDecimal.valueOf(p4thousandCount * 1000));

            Integer p4hundredCount = p4.divide(BigDecimal.valueOf(100)).intValue();
            item.setHundred4(p4hundredCount);
            p4 = p4.subtract(BigDecimal.valueOf(p4hundredCount * 100));

            Integer p4tenCount = p4.divide(BigDecimal.valueOf(10)).intValue();
            item.setTen4(p4tenCount);
            p4 = p4.subtract(BigDecimal.valueOf(p4tenCount * 10));

            Integer p4yuanCount = p4.divide(BigDecimal.valueOf(1)).intValue();
            item.setYuan4(p4yuanCount);
            p4 = p4.subtract(BigDecimal.valueOf(p4yuanCount * 1));

            Integer p4jiaoCount = p4.divide(BigDecimal.valueOf(0.1)).intValue();
            item.setJiao4(p4jiaoCount);
            p4 = p4.subtract(BigDecimal.valueOf(p4jiaoCount * 0.1));

            Integer p4fenCount = p4.divide(BigDecimal.valueOf(0.01)).intValue();
            item.setFen4(p4fenCount);
        }

        //项目5
        if (StringUtils.isNotEmpty(item.getProject5())) {
            BigDecimal p5 = BigDecimalUtil.getData(item.getCost5()).add(BigDecimalUtil.getData(item.getFee5()));

            Integer p5million3Count = p5.divide(BigDecimal.valueOf(1000000)).intValue();
            item.setMillion35(p5million3Count);
            p5 = p5.subtract(BigDecimal.valueOf(p5million3Count * 1000000));

            Integer p5million2Count = p5.divide(BigDecimal.valueOf(100000)).intValue();
            item.setMillion25(p5million2Count);
            p5 = p5.subtract(BigDecimal.valueOf(p5million2Count * 100000));

            Integer p5million1Count = p5.divide(BigDecimal.valueOf(10000)).intValue();
            item.setMillion15(p5million1Count);
            p5 = p5.subtract(BigDecimal.valueOf(p5million1Count * 10000));

            Integer p5thousandCount = p5.divide(BigDecimal.valueOf(1000)).intValue();
            item.setThousand5(p5thousandCount);
            p5 = p5.subtract(BigDecimal.valueOf(p5thousandCount * 1000));

            Integer p5hundredCount = p5.divide(BigDecimal.valueOf(100)).intValue();
            item.setHundred5(p5hundredCount);
            p5 = p5.subtract(BigDecimal.valueOf(p5hundredCount * 100));

            Integer p5tenCount = p5.divide(BigDecimal.valueOf(10)).intValue();
            item.setTen5(p5tenCount);
            p5 = p5.subtract(BigDecimal.valueOf(p5tenCount * 10));

            Integer p5yuanCount = p5.divide(BigDecimal.valueOf(1)).intValue();
            item.setYuan5(p5yuanCount);
            p5 = p5.subtract(BigDecimal.valueOf(p5yuanCount * 1));

            Integer p5jiaoCount = p5.divide(BigDecimal.valueOf(0.1)).intValue();
            item.setJiao5(p5jiaoCount);
            p5 = p5.subtract(BigDecimal.valueOf(p5jiaoCount * 0.1));

            Integer p5fenCount = p5.divide(BigDecimal.valueOf(0.01)).intValue();
            item.setFen5(p5fenCount);
        }

        //项目6
        if (StringUtils.isNotEmpty(item.getProject6())) {
            BigDecimal p6 = BigDecimalUtil.getData(item.getCost6()).add(BigDecimalUtil.getData(item.getFee6()));

            Integer p6million3Count = p6.divide(BigDecimal.valueOf(1000000)).intValue();
            item.setMillion36(p6million3Count);
            p6 = p6.subtract(BigDecimal.valueOf(p6million3Count * 1000000));

            Integer p6million2Count = p6.divide(BigDecimal.valueOf(100000)).intValue();
            item.setMillion26(p6million2Count);
            p6 = p6.subtract(BigDecimal.valueOf(p6million2Count * 100000));

            Integer p6million1Count = p6.divide(BigDecimal.valueOf(10000)).intValue();
            item.setMillion16(p6million1Count);
            p6 = p6.subtract(BigDecimal.valueOf(p6million1Count * 10000));

            Integer p6thousandCount = p6.divide(BigDecimal.valueOf(1000)).intValue();
            item.setThousand6(p6thousandCount);
            p6 = p6.subtract(BigDecimal.valueOf(p6thousandCount * 1000));

            Integer p6hundredCount = p6.divide(BigDecimal.valueOf(100)).intValue();
            item.setHundred6(p6hundredCount);
            p6 = p6.subtract(BigDecimal.valueOf(p6hundredCount * 100));

            Integer p6tenCount = p6.divide(BigDecimal.valueOf(10)).intValue();
            item.setTen6(p6tenCount);
            p6 = p6.subtract(BigDecimal.valueOf(p6tenCount * 10));

            Integer p6yuanCount = p6.divide(BigDecimal.valueOf(1)).intValue();
            item.setYuan6(p6yuanCount);
            p6 = p6.subtract(BigDecimal.valueOf(p6yuanCount * 1));

            Integer p6jiaoCount = p6.divide(BigDecimal.valueOf(0.1)).intValue();
            item.setJiao6(p6jiaoCount);
            p6 = p6.subtract(BigDecimal.valueOf(p6jiaoCount * 0.1));

            Integer p6fenCount = p6.divide(BigDecimal.valueOf(0.01)).intValue();
            item.setFen6(p6fenCount);
        }

        return item;
    }

    /**
     * 分页
     *
     * @param params
     * @return
     */
    public PageResult<BillGhsfpt> pageAll(Map<String, Object> params) {
        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
        if (MapUtils.getInteger(params, "page") != null && MapUtils.getInteger(params, "limit") != null)
            PageHelper.startPage(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"), true);

        List<BillGhsfpt> list = billGhsfptDao.findAll(params);
        PageInfo<BillGhsfpt> pageInfo = new PageInfo(list);

        return PageResult.<BillGhsfpt>builder().data(pageInfo.getList()).resp_code(CodeEnum.SUCCESS.getCode()).count(pageInfo.getTotal()).build();
    }

    /**
     * 列表
     *
     * @param params
     * @return
     */
    public List<BillGhsfpt> findAll(Map<String, Object> params) {
        return billGhsfptDao.findAll(params);
    }

}
